package com.example.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sys.entity.Article;
import com.example.sys.entity.dto.ArticleDto;
import com.example.sys.entity.User;
import com.example.sys.entity.forms.ArticleSaveForm;
import com.example.sys.mapper.ArticleMapper;
import com.example.sys.mapper.UserMapper;
import com.example.sys.service.ArticleService;
import com.github.yulichang.toolkit.MPJWrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ZhengDing
 * @since 2022-01-28 09:31:35
 */
@RestController
@RequestMapping("/sys/article")
public class ArticleController {
    
    @Autowired
    ArticleService articleService;
    
    @Autowired
    ArticleMapper articleMapper;
    
    @Autowired
    UserMapper userMapper;
    
    @GetMapping("list")
    public List<Article> list() {
        List<Article> articles = articleService.list();
        System.out.println(articles);
        
        return articles;
    }
    
    @GetMapping("list2")
    public Object list2() {
        //MPJLambdaWrapper<ArticleDto> wrapper = new MPJLambdaWrapper<>();
        //wrapper.selectAll(Article.class)
        //        .selectAll(User.class).
        //        leftJoin(User.class, User::getId, Article::getCreateBy);
        //List<ArticleDto> articleList = articleService.selectJoinList(ArticleDto.class, wrapper);
    
        IPage<ArticleDto> page = articleMapper.selectJoinPage(new Page<>(1, 10), ArticleDto.class,
                MPJWrappers.<Article>lambdaJoin()
                        .selectAll(Article.class)
                        .select(User::getUsername)
                        .select(User::getPassword)
                        .leftJoin(User.class, User::getId, Article::getCreateBy));
    
        return page;
    }
    
    @GetMapping("list3")
    public Object list3() {
        //List<Article> articles = articleMapper.selectListDeep(new QueryWrapper<>());
        //return articles;
    
        Article result = articleMapper.selectByIdDeep(2);
        return result;
    }
    
    @PostMapping("save")
    public String save(@RequestBody ArticleSaveForm art) {
        Article article = new Article();
        BeanUtils.copyProperties(art, article);
        //article.setCreateBy(UserUtil.createWithId(art.getCreateBy()));
        System.out.println(article);
        boolean save = articleService.save(article);
        System.out.println(art);
        return "success";
    }
}
